package cn.lili.modules.serviceimpl;

import cn.lili.common.utils.StringUtils;
import cn.lili.modules.dto.PublisherDTO;
import cn.lili.modules.entity.AppOutside;
import cn.lili.modules.entity.AppUser;
import cn.lili.modules.entity.AppVideosCeshi;
import cn.lili.modules.mapper.AppOutsideMapper;
import cn.lili.modules.mapper.AppVideosCeshiMapper;
import cn.lili.modules.service.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:xiangqian
 * @date: 2024/3/8 9:20
 */
@Service
public class AppVideosCeshiServiceImpl extends ServiceImpl<AppVideosCeshiMapper, AppVideosCeshi> implements AppVideosCeshiService {

    @Resource
    private AppUserService appUserService;
    @Resource
    private AppOutsideMapper appOutsideMapper;
    @Resource
    private AppUsersFansService appUsersFansService;
    @Resource
    private AppGiveService appGiveService;
    @Resource
    private AppUsersLikeVideosService appUsersLikeVideosService;

    /**
     * 视频信息分页查询
     *
     * @param appVideosCeshi 查询条件
     * @return 返回视频分页
     */
    @Override
    public Page<AppVideosCeshi> page(AppVideosCeshi appVideosCeshi) {
        //查询当前用户信息
        AppUser appUser = appUserService.getAppUserByTokenNot(appVideosCeshi.getToken());

        //查询视频信息
        Page<AppVideosCeshi> page = this.page(new Page<>(appVideosCeshi.getPage(), appVideosCeshi.getPage_size()), appVideosCeshi.wrapper(appUser==null?null:appUser.getOpenid()));
        if(page == null || page.getRecords().size() < 10){
            page = this.page(new Page<>(appVideosCeshi.getPage(), appVideosCeshi.getPage_size()), appVideosCeshi.wrapper2(appUser==null?null:appUser.getOpenid()));
        }

        //重新赋值
        List<AppVideosCeshi> resultList = new ArrayList<>();
        for (AppVideosCeshi appVideosCeshi1 : page.getRecords()) {
            if (appUser != null) {
                //进行是否关注查询
                Long usersFansCount = appUsersFansService.getAppUsersFansCount(appUser.getOpenid(), appVideosCeshi1.getUser_id());
                appVideosCeshi1.setUsers_fans(Integer.parseInt(String.valueOf(usersFansCount)));
                //进行是否点赞查询
                Long appGiveCount = appGiveService.getAppGiveCount(appUser.getOpenid(), appVideosCeshi1.getId().toString());
                appVideosCeshi1.setGive(Integer.parseInt(String.valueOf(appGiveCount)));
                //是否收藏
                Long appUsersLikeVideosCount = appUsersLikeVideosService.getAppUsersLikeVideosCount(appUser.getOpenid(), appVideosCeshi1.getId().toString());
                appVideosCeshi1.setShoucang(Integer.parseInt(String.valueOf(appUsersLikeVideosCount)));
            }
            //查询用户数据
            AppUser appUserBy = appUserService.getAppUserBy(appVideosCeshi1.getUser_id());
            if(appUserBy != null){
                appVideosCeshi1.setPublisher(PublisherDTO.setPublisher(appUserBy));
            }
            resultList.add(appVideosCeshi1);
        }

        page.setRecords(resultList);
        return page;
    }

    /**
     * 上传视频
     *
     * @param appVideosCeshi
     * @return 上传是否成功
     */
    @Override
    public Boolean saveVideo(AppVideosCeshi appVideosCeshi) {
        //根据token查询用户信息
        AppUser appUser = appUserService.getAppUserByToken(appVideosCeshi.getToken());

        //设置视频保存信息
        appVideosCeshi.setVideo_path("http://video.chainlettre12.com/" + appVideosCeshi.getFileid());
        appVideosCeshi.setVideo_seconds(appVideosCeshi.getVideo_seconds() == null ? 0 : appVideosCeshi.getVideo_seconds());
        appVideosCeshi.setVideo_path(StringUtils.isEmpty(appVideosCeshi.getVideo_path()) ? "" : appVideosCeshi.getVideo_path());
        if (appVideosCeshi.getOutside_id() != null && appVideosCeshi.getOutside_id() != 0) {
            AppOutside appOutside = appOutsideMapper.selectById(appVideosCeshi.getOutside_id());
            if (appOutside != null) {
                appVideosCeshi.setAdv_name(appOutside.getName());
                appVideosCeshi.setAdv_url(appOutside.getUrls());
            }
        }
        appVideosCeshi.setUser_id(appUser.getOpenid());
        appVideosCeshi.setCreate_time((int) (System.currentTimeMillis() / 1000));
        appVideosCeshi.setAlbum_id(appVideosCeshi.getAlbum_id());
        appVideosCeshi.setAlbum_name(appVideosCeshi.getAlbum_name());

        return save(appVideosCeshi);
    }
}
